package com.android.memory;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MemoryActivity extends Activity {
  
	protected boolean _active = true;
	protected int _splashTime = 2000; // time to display the splash screen in ms
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //Fullscreen mode
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.splash);
        
        // Reads in the values for options preferences
        SharedPreferences settings = getSharedPreferences( OptionsActivity.PREFS_NAME, 0);
        int diff = settings.getInt("difficulty", 2 );
        OptionsActivity.musicPlayer = settings.getBoolean("music", true);
        OptionsActivity.soundPlayer = settings.getBoolean("sound",true);
        GameActivity.setDifficulty(diff);

        //Starts the music if enabled in options
        if ((OptionsActivity.musicPlayer)==true){
    	Intent backgroundmusic= new Intent(this, MusicManagerService.class);
        startService(backgroundmusic);
        }

        //Animation for the splash screen
        final View tv = findViewById(R.id.splashName);
        Animation a = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        a.setDuration(5000);
        tv.startAnimation(a);
        
        // thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    Intent i = new Intent();
                    i.setClassName("com.android.memory",
                                   "com.android.memory.MainMenuActivity");
                    startActivity(i);
                    stop();
                    //Overrides the transition animation. Making if fade instead
                    MemoryActivity.this.overridePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);
                    
                }
            }
        };
        splashTread.start();
    }
      
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
    
    public void onStop() {
		super.onStop();
		if(!MusicManagerService.getContinueMusic())
		{
	    	Intent backgroundmusic= new Intent(this, MusicManagerService.class);
			stopService(backgroundmusic);
		}
	}
	
	public void onPause() {
		super.onPause();
		MusicManagerService.setContinueMusic(false);
	}
	
	public void onResume() {
		super.onResume();
	}
	
	public void onDestroy() {
		super.onDestroy();		
	}
    
}
